package com.ias.common.utils.datetime;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.Date;

/**
 * DateTimeUtil 使用示例
 * 
 * @author IAS Platform
 */
public class DateTimeUtilExample {
    
    public static void main(String[] args) {
        System.out.println("=== DateTimeUtil 功能演示 ===\n");
        
        // 1. 获取当前时间
        demonstrateCurrentTime();
        
        // 2. 时间格式化
        demonstrateFormatting();
        
        // 3. 时间解析
        demonstrateParsing();
        
        // 4. 时间戳转换
        demonstrateTimestampConversion();
        
        // 5. 时间计算
        demonstrateTimeCalculation();
        
        // 6. 时间比较
        demonstrateTimeComparison();
        
        // 7. 特殊日期获取
        demonstrateSpecialDates();
        
        // 8. 时间验证
        demonstrateTimeValidation();
        
        // 9. 实用工具
        demonstrateUtilityMethods();
        
        System.out.println("\n=== 演示完成 ===");
    }
    
    private static void demonstrateCurrentTime() {
        System.out.println("1. 获取当前时间:");
        System.out.println("当前日期: " + DateTimeUtil.now());
        System.out.println("当前时间: " + DateTimeUtil.nowTime());
        System.out.println("当前日期时间: " + DateTimeUtil.nowDateTime());
        System.out.println("当前时间戳(毫秒): " + DateTimeUtil.nowTimestamp());
        System.out.println("当前时间戳(秒): " + DateTimeUtil.nowTimestampSeconds());
        System.out.println();
    }
    
    private static void demonstrateFormatting() {
        System.out.println("2. 时间格式化:");
        LocalDate date = LocalDate.of(2024, 12, 25);
        LocalTime time = LocalTime.of(15, 30, 45);
        LocalDateTime dateTime = LocalDateTime.of(2024, 12, 25, 15, 30, 45);
        
        System.out.println("默认日期格式: " + DateTimeUtil.formatDate(date));
        System.out.println("中文日期格式: " + DateTimeUtil.formatDate(date, DateTimeUtil.CHINESE_DATE_FORMAT));
        System.out.println("紧凑日期格式: " + DateTimeUtil.formatDate(date, DateTimeUtil.COMPACT_DATE_FORMAT));
        
        System.out.println("默认时间格式: " + DateTimeUtil.formatTime(time));
        
        System.out.println("默认日期时间格式: " + DateTimeUtil.formatDateTime(dateTime));
        System.out.println("ISO日期时间格式: " + DateTimeUtil.formatDateTime(dateTime, DateTimeUtil.ISO_DATETIME_FORMAT));
        System.out.println("中文日期时间格式: " + DateTimeUtil.formatDateTime(dateTime, DateTimeUtil.CHINESE_DATETIME_FORMAT));
        System.out.println();
    }
    
    private static void demonstrateParsing() {
        System.out.println("3. 时间解析:");
        String dateStr = "2024-12-25";
        String timeStr = "15:30:45";
        String dateTimeStr = "2024-12-25 15:30:45";
        String chineseDateStr = "2024年12月25日";
        
        System.out.println("解析日期 '" + dateStr + "': " + DateTimeUtil.parseDate(dateStr));
        System.out.println("解析时间 '" + timeStr + "': " + DateTimeUtil.parseTime(timeStr));
        System.out.println("解析日期时间 '" + dateTimeStr + "': " + DateTimeUtil.parseDateTime(dateTimeStr));
        System.out.println("解析中文日期 '" + chineseDateStr + "': " + 
                         DateTimeUtil.parseDate(chineseDateStr, DateTimeUtil.CHINESE_DATE_FORMAT));
        System.out.println();
    }
    
    private static void demonstrateTimestampConversion() {
        System.out.println("4. 时间戳转换:");
        long timestamp = System.currentTimeMillis();
        LocalDateTime dateTime = LocalDateTime.now();
        
        System.out.println("时间戳 " + timestamp + " 转日期时间: " + DateTimeUtil.timestampToDateTime(timestamp));
        System.out.println("日期时间 " + dateTime + " 转时间戳: " + DateTimeUtil.dateTimeToTimestamp(dateTime));
        
        // Date与LocalDateTime转换
        Date date = new Date();
        LocalDateTime convertedDateTime = DateTimeUtil.dateToLocalDateTime(date);
        Date convertedDate = DateTimeUtil.localDateTimeToDate(convertedDateTime);
        
        System.out.println("Date转LocalDateTime: " + date + " -> " + convertedDateTime);
        System.out.println("LocalDateTime转Date: " + convertedDateTime + " -> " + convertedDate);
        System.out.println();
    }
    
    private static void demonstrateTimeCalculation() {
        System.out.println("5. 时间计算:");
        LocalDate date = LocalDate.now();
        LocalDateTime dateTime = LocalDateTime.now();
        
        System.out.println("今天: " + date);
        System.out.println("加5天: " + DateTimeUtil.plusDays(date, 5));
        System.out.println("减3天: " + DateTimeUtil.minusDays(date, 3));
        
        System.out.println("现在: " + dateTime);
        System.out.println("加2小时: " + DateTimeUtil.plusHours(dateTime, 2));
        System.out.println("减30分钟: " + DateTimeUtil.minusMinutes(dateTime, 30));
        System.out.println();
    }
    
    private static void demonstrateTimeComparison() {
        System.out.println("6. 时间比较和时间差:");
        LocalDate date1 = LocalDate.of(2024, 1, 1);
        LocalDate date2 = LocalDate.of(2024, 12, 31);
        LocalDateTime dateTime1 = LocalDateTime.of(2024, 1, 1, 10, 0);
        LocalDateTime dateTime2 = LocalDateTime.of(2024, 1, 1, 15, 30);
        
        System.out.println("日期1: " + date1 + ", 日期2: " + date2);
        System.out.println("date1 < date2: " + DateTimeUtil.isBefore(date1, date2));
        System.out.println("date1 > date2: " + DateTimeUtil.isAfter(date1, date2));
        System.out.println("两日期相差天数: " + DateTimeUtil.daysBetween(date1, date2));
        
        System.out.println("日期时间1: " + dateTime1 + ", 日期时间2: " + dateTime2);
        System.out.println("相差小时数: " + DateTimeUtil.hoursBetween(dateTime1, dateTime2));
        System.out.println("相差分钟数: " + DateTimeUtil.minutesBetween(dateTime1, dateTime2));
        System.out.println();
    }
    
    private static void demonstrateSpecialDates() {
        System.out.println("7. 特殊日期获取:");
        LocalDate date = LocalDate.of(2024, 6, 15);
        LocalDateTime dateTime = LocalDateTime.of(2024, 6, 15, 14, 30, 45);
        
        System.out.println("基准日期: " + date);
        System.out.println("月份第一天: " + DateTimeUtil.getFirstDayOfMonth(date));
        System.out.println("月份最后一天: " + DateTimeUtil.getLastDayOfMonth(date));
        System.out.println("年份第一天: " + DateTimeUtil.getFirstDayOfYear(date));
        System.out.println("年份最后一天: " + DateTimeUtil.getLastDayOfYear(date));
        
        System.out.println("基准日期时间: " + dateTime);
        System.out.println("当天开始时间: " + DateTimeUtil.getStartOfDay(dateTime));
        System.out.println("当天结束时间: " + DateTimeUtil.getEndOfDay(dateTime));
        System.out.println();
    }
    
    private static void demonstrateTimeValidation() {
        System.out.println("8. 时间验证:");
        LocalDate today = LocalDate.now();
        LocalDate testDate = LocalDate.of(2024, 6, 15); // 假设这是个周六
        
        System.out.println("2024年是闰年: " + DateTimeUtil.isLeapYear(2024));
        System.out.println("2023年是闰年: " + DateTimeUtil.isLeapYear(2023));
        System.out.println("今天是: " + today + ", 是否是今天: " + DateTimeUtil.isToday(today));
        System.out.println("测试日期: " + testDate + ", 是否是周末: " + DateTimeUtil.isWeekend(testDate));
        System.out.println();
    }
    
    private static void demonstrateUtilityMethods() {
        System.out.println("9. 实用工具方法:");
        
        // 年龄计算
        LocalDate birthDate = LocalDate.of(1990, 5, 15);
        System.out.println("出生日期: " + birthDate + ", 年龄: " + DateTimeUtil.getAge(birthDate) + "岁");
        
        // 日期信息
        LocalDate date = LocalDate.of(2024, 6, 15);
        System.out.println("日期: " + date);
        System.out.println("星期几: " + DateTimeUtil.getDayOfWeek(date) + " (1=周一, 7=周日)");
        System.out.println("一年中第几天: " + DateTimeUtil.getDayOfYear(date));
        System.out.println("所在月份天数: " + DateTimeUtil.getDaysInMonth(date));
        System.out.println("2024年总天数: " + DateTimeUtil.getDaysInYear(2024));
        
        // 持续时间格式化
        long duration1 = 5 * 24 * 60 * 60 * 1000 + 3 * 60 * 60 * 1000 + 30 * 60 * 1000; // 5天3小时30分钟
        long duration2 = 2 * 60 * 1000 + 500; // 2分钟500毫秒
        System.out.println("持续时间1 (" + duration1 + "ms): " + DateTimeUtil.formatDuration(duration1));
        System.out.println("持续时间2 (" + duration2 + "ms): " + DateTimeUtil.formatDuration(duration2));
        
        // 时区转换示例
        LocalDateTime localTime = LocalDateTime.now();
        LocalDateTime utcTime = DateTimeUtil.toUTC(localTime);
        LocalDateTime backToLocal = DateTimeUtil.fromUTC(utcTime);
        
        System.out.println("本地时间: " + localTime);
        System.out.println("UTC时间: " + utcTime);
        System.out.println("转回本地: " + backToLocal);
    }
} 